package coolapk.cj.cj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import coolapk.cj.cj.entity.Goods;
import coolapk.cj.cj.service.GoodsService;
import coolapk.cj.cj.utils.BindingResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wenjunjun9
 * @created 2021-02-16 18:54
 * @description
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/addOrUpdate")
    public ResponseEntity<String> addOrUpdate(@RequestBody @Valid Goods goods, BindingResult bindingResult) {
        BindingResultUtil.apply(bindingResult);

        goodsService.addOrUpdateGoods(goods);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/delete")
    public ResponseEntity<String> delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new IllegalArgumentException("id不能为空！");
        }

        goodsService.deleteGoods(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/page")
    public ResponseEntity<IPage<Goods>> page(@RequestBody Page<Goods> page) {
        IPage<Goods> goodsIPage = goodsService.pageQuery(page);
        return ResponseEntity.ok(goodsIPage);
    }

    @PostMapping("/pageWinner")
    public ResponseEntity<IPage<Goods>> pageWinner(@RequestBody Page<Goods> page) {
        IPage<Goods> goodsIPage = goodsService.pageWinner(page);
        return ResponseEntity.ok(goodsIPage);
    }

    @PostMapping("/current")
    public ResponseEntity<List<Goods>> listCurrent() {
        List<Goods> list = goodsService.queryCurrentGoods();
        return ResponseEntity.ok(list);
    }
}
